/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author SSANCHEZ
 */
package demo.web;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.imageio.stream.FileImageOutputStream;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Image;

 
 
@ManagedBean(name="editorUsuarioFoto")
@ViewScoped
public class SubirImagen {
 
private String imageTemp;
private Image croppeFoto;
 
public void actionFoto(){
    this.imageTemp = null;
}
 
public void actionGuardarFoto(){
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/usuario");
    String archivo = path + File.separator+ ".jpg";
 
    try {
 
        if(croppeFoto!=null){
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(archivo));
            imageOutput.write(croppeFoto.getContent().getByteData(), 0, croppeFoto.getContent().getByteData().length);
            imageOutput.close();
        }
        else {
            OutputStream outStream = new FileOutputStream(new File(archivo));
            InputStream inputStream = new FileInputStream(path+ "/temp/" + this.imageTemp);
            byte[] buffer = new byte[6124];
            int bulk;
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                outStream.write(buffer, 0, bulk);
                outStream.flush();
            }
            outStream.close();
            inputStream.close();
        }
 
        actionFoto();
 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
public void uploadFile(UploadEvent event) {
    try {
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/usuario/temp");
        String archivo = path + File.separator + event.getName();
 
        FileOutputStream fileOutputStream = new FileOutputStream(archivo);
        byte[] buffer = new byte[6124];
        int bulk;
        InputStream inputStream = event.getMedia().getStreamData();
        while (true) {
        bulk = inputStream.read(buffer);
        if (bulk < 0) {
          break;
        }
        fileOutputStream.write(buffer, 0, bulk);
        fileOutputStream.flush();
    }
    fileOutputStream.close();
    inputStream.close();
 
 
    } catch (IOException e) {
        e.printStackTrace();
        FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Error al subir el archivo"));
    }
}
 
//setter and getter
}